/**
 * 
 */
package com.yonder.agenda.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import com.yonder.agenda.entity.generic.AbstractEntity;

/**
 * {@link Contact} entity.
 * 
 * @author CosminS
 * @since Jan 28, 2014
 */
@Entity
@Table(name = "contacts")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Contact extends AbstractEntity {

	/**
	 * First name of the contact.
	 */
	@Column(name = "first_name")
	private String firstName;

	/**
	 * Last name of the contact.
	 */
	@Column(name = "last_name")
	private String lastName;

	/**
	 * Telephones list associated with the specified contact.
	 */
	@LazyCollection(LazyCollectionOption.FALSE)
	@OneToMany(mappedBy = "contact")
	@XmlTransient
	private List<Telephone> telephones;

	/**
	 * Telephones list associated with the specified contact.
	 */
	@LazyCollection(LazyCollectionOption.FALSE)
	@OneToMany(mappedBy = "contact")
	@XmlTransient
	private List<Email> emails;

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public List<Telephone> getTelephones() {
		return telephones;
	}

	public void setTelephones(List<Telephone> telephones) {
		this.telephones = telephones;
	}

	public List<Email> getEmails() {
		return emails;
	}

	public void setEmails(List<Email> emails) {
		this.emails = emails;
	}

	/**
	 * Add a telephone to the contact.
	 * 
	 * @param telephone
	 *            telephone to be added to the contact.
	 */
	public void addTelephone(Telephone telephone) {
		if (telephone == null) {
			return;
		}

		if (telephones == null) {
			setTelephones(new ArrayList<Telephone>());
		}
		telephone.setContact(this);
		getTelephones().add(telephone);
	}

	/**
	 * Add an e-mail to the contact.
	 * 
	 * @param email
	 *            e-mail to be added to the contact.
	 */
	public void addEmail(Email email) {
		if (email == null) {
			return;
		}

		if (emails == null) {
			setEmails(new ArrayList<Email>());
		}
		email.setContact(this);
		getEmails().add(email);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((emails == null) ? 0 : emails.hashCode());
		result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
		result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
		result = prime * result + ((telephones == null) ? 0 : telephones.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Contact)) {
			return false;
		}
		Contact other = (Contact) obj;
		if (emails == null) {
			if (other.emails != null) {
				return false;
			}
		} else if (!emails.equals(other.emails)) {
			return false;
		}
		if (firstName == null) {
			if (other.firstName != null) {
				return false;
			}
		} else if (!firstName.equals(other.firstName)) {
			return false;
		}
		if (lastName == null) {
			if (other.lastName != null) {
				return false;
			}
		} else if (!lastName.equals(other.lastName)) {
			return false;
		}
		if (telephones == null) {
			if (other.telephones != null) {
				return false;
			}
		} else if (!telephones.equals(other.telephones)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Contact [firstName=");
		builder.append(firstName);
		builder.append(", lastName=");
		builder.append(lastName);
		builder.append("]");
		return builder.toString();
	}
}